<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>资讯管理</title>
    <link rel="stylesheet" href="../../assets/libs/layui/css/layui.css">
</head>
<body>
<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<img id="i"/>
</body>
<script src="../../assets/libs/jquery/jquery-3.2.1.min.js"></script>
<script src="../../assets/libs/layui/layui.js"></script>
<script src="../../assets/js/my.js"></script>
<script>
    function show_img(t) {
        var t = $(t).find("img");
        //页面层
        layer.open({
            type: 1,
            skin: 'layui-layer-rim', //加上边框
            area: ['80%', '80%'], //宽高
            shadeClose: true, //开启遮罩关闭
            end: function (index, layero) {
                return false;
            },
            content: '<div style="text-align:center"><img src="' + $(t).attr('src') + '" /></div>'
        });
    }
    layui.use(['table', 'jquery', 'layer'], function () {
        var table = layui.table;
        var $ = layui.jquery;
        var layer = layui.layer


        table.render({
            elem: '#test'
            , url: host+'/newsservlet/list'
            , toolbar: '#toolbarDemo'
            , title: '员工数据表'
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'nId', title: 'ID', width: 80, fixed: 'left', unresize: true, sort: true}
                , {field: 'title', title: '标题', width: 120, edit: 'text'}
                , {field: 'author', title: '作者', width: 120, edit: 'text'}
                , {field: 'summary', title: '摘要', width: 80, edit: 'text', sort: true}
                , {field: 'content', title: '内容', width: 80, edit: 'text', sort: true}
                , {field: 'path', title: '封面图片', sort: true, templet: function (d) {
                        return '<div onclick="show_img(this)" ><img src="../upload/' + d.path.substr(47) + '" alt="" width="50px" height="50px"></a></div>';
                    }}
                    ,{field:'pname',title:'照片名称',sort:true}
                , {field: 'time', title: '创建/修改时间', width: 150, edit: 'text'}
                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150}
            ]]
            , page: true
        });

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'add':
                    layer.open({
                        type:2,
                        content:['article-add.html'],
                        area:['600px','400px'],
                        maxmin:true,
                        cancel:function (index){
                            $(".layui-laypage-btn").click();//这里用于关闭Open时触发回调函数  刷新父页面数据  一定要引入Jquery
                        }
                    })
                    break;
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除么', function(index){
                    obj.del();
                    layer.close(index);
                    // alert(data.uId);
                    $.get(host+'/newsservlet/removeById',{nId:data.nId},function (r) {
                        if(r.code==0){
                            layer.msg("删除成功");
                        }else{
                            layer.msg("删除失败");
                        }
                    },'json')
                });
            } else if(obj.event === 'edit'){
                    layer.open({
                        type:2,
                        content:['article-update.html'],
                        area:['600px','400px'],
                        maxmin: true,
                        success:function (layero,index) {
                            var body = layer.getChildFrame('body', index);
                            // var iframeWin = window[layero.find('iframe')[0]['name']];
                            body.find("#nId").val(data.nId);
                            body.find("#title").val(data.title);
                            body.find("#summary").val(data.summary);
                            body.find("#content").val(data.content);
                            body.find("#path").val(data.path);
                            body.find("#date1").val(data.time);
                            body.find("#author").val(data.author);

                        },

                        cancel:function (index){
                            $(".layui-laypage-btn").click();//这里用于关闭Open时触发回调函数  刷新父页面数据  一定要引入Jquery
                        }
                });
            }
        });

    });
</script>
</html>